# -*- coding: utf-8 -*-..,
##############################################################################
#
#    @author: PhongND
#    Copyright (C) 2014 Crea8s (http://www.crea8s.com) All Rights Reserved.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################


# -*- coding: UTF-8 -*-
'''
    magento

    :copyright: (c) 2013-2014 by crea8s
    :license: AGPLv3, see LICENSE for more details
'''
import magento
from openerp.osv import fields, osv
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp


class Category(osv.Model):
    """Product Category
    """
    _inherit = 'product.category'

    _columns = dict(
        crea8s_magento_ids=fields.one2many(
            'crea8s.magento.instance.product_category', 'category',
            string='Magento IDs',
        ),
    )

    def create_tree_using_magento_data(
        self, cursor, user, category_tree, context
    ):
        """Create the categories from the category tree

        :param cursor: Database cursor
        :param user: ID of current user
        :param category_tree: Category Tree from magento
        :param context: Application context
        """
        
        return True
#         # Create the root
#         root_categ = self.find_or_create_using_magento_data(
#             cursor, user, category_tree, context=context
#         )
#         for child in category_tree['children']:
#             self.find_or_create_using_magento_data(
#                 cursor, user, child, parent=root_categ.id, context=context
#             )
#             if child['children']:
#                 self.create_tree_using_magento_data(
#                     cursor, user, child, context
#                 )


    def find_or_create_using_magento_data(
        self, cursor, user, category_data, parent=None, context=None
    ):
        """Find or Create category using magento data

        :param cursor: Database cursor
        :param user: ID of current user
        :param category_data: Category Data from magento
        :param parent: openerp ID of parent if present else None
        :param context: Application context
        :returns: Browse record of category found/created
        """
        
        return True
#         category = self.find_using_magento_data(
#             cursor, user, category_data, context
#         )
#         if not category:
#             category = self.create_using_magento_data(
#                 cursor, user, category_data, parent, context
#             )
# 
#         return category

    def find_or_create_using_magento_id(
        self, cursor, user, magento_id, parent=None, context=None
    ):
        """Find or Create category using magento ID of category

        :param cursor: Database cursor
        :param user: ID of current user
        :param magento_id: Category ID from magento
        :param parent: openerp ID of parent if present else None
        :param context: Application context
        :returns: Browse record of category found/created
        """
        instance_obj = self.pool.get('crea8s.magento.instance')
        return True
#         category = self.find_using_magento_id(
#             cursor, user, magento_id, context
#         )
#         if not category:
#             instance = instance_obj.browse(
#                 cursor, user, context['magento_instance'], context=context
#             )
# 
#             with magento.Category(
#                 instance.url, instance.api_user, instance.api_key
#             ) as category_api:
#                 category_data = category_api.info(magento_id)
# 
#             category = self.create_using_magento_data(
#                 cursor, user, category_data, parent, context
#             )
# 
#         return category

    def find_using_magento_data(
        self, cursor, user, category_data, context=None
    ):
        """Find category using magento data

        :param cursor: Database cursor
        :param user: ID of current user
        :param category_data: Category Data from magento
        :param context: Application context
        :returns: Browse record of category found or None
        """
        return True
#         magento_category_obj = self.pool.get(
#             'crea8s.magento.instance.product_category'
#         )
#         record_ids = magento_category_obj.search(cursor, user, [
#             ('magento_id', '=', int(category_data['category_id'])),
#             ('instance', '=', context['magento_instance'])
#         ], context=context)
#         return record_ids and magento_category_obj.browse(
#             cursor, user, record_ids[0], context=context
#         ).category or None

    def find_using_magento_id(
        self, cursor, user, magento_id, context=None
    ):
        """Find category using magento id or category

        :param cursor: Database cursor
        :param user: ID of current user
        :param magento_id: Category ID from magento
        :param context: Application context
        :returns: Browse record of category found or None
        """
        return True
#         magento_category_obj = self.pool.get(
#             'crea8s.magento.instance.product_category'
#         )
#         record_ids = magento_category_obj.search(cursor, user, [
#             ('magento_id', '=', magento_id),
#             ('instance', '=', context['magento_instance'])
#         ], context=context)
#         return record_ids and magento_category_obj.browse(
#             cursor, user, record_ids[0], context=context
#         ).category or None


    def create_using_magento_data(
        self, cursor, user, category_data, parent=None, context=None
    ):
        """Create category using magento data

        :param cursor: Database cursor
        :param user: ID of current user
        :param category_data: Category Data from magento
        :param parent: openerp ID of parent if present else None
        :param context: Application context
        :returns: Browse record of category created
        """
        return True
#         category_id = self.create(cursor, user, {
#             'name': category_data['name'],
#             'parent_id': parent,
#             'crea8s_magento_ids': [(0, 0, {
#                 'magento_id': int(category_data['category_id']),
#                 'instance': context['magento_instance'],
#             })]
#         }, context=context)
# 
#         return self.browse(cursor, user, category_id, context=context)


class MagentoInstanceCategory(osv.Model):
    """Magento Instance - Product category store

    This model keeps a record of a category's association with an instance and
    the ID of category on that instance
    """
    _name = 'crea8s.magento.instance.product_category'
    _description = 'Magento Instance - Product category store'

    _columns = dict(
        magento_id=fields.integer(
            'Magento ID', required=True, select=True,
        ),
        instance=fields.many2one(
            'crea8s.magento.instance', 'Magento Instance', readonly=True,
            select=True, required=True
        ),
        category=fields.many2one(
            'product.category', 'Product Category', readonly=True,
            required=True, select=True
        )
    )

#     _sql_constraints = [
#         (
#             'magento_id_instance_unique',
#             'unique(magento_id, instance)',
#             'Each category in an instance must be unique!'
#         ),
#     ]

